import pandas as pd
import os

# 处理3D数据文件编码问题
def fix_encoding():
    try:
        # 尝试不同的编码方式
        encodings = ['utf-8', 'gbk', 'gb2312', 'latin1', 'cp936']
        
        for encoding in encodings:
            try:
                print(f"尝试编码: {encoding}")
                df = pd.read_csv('g:\\python\\caipiao\\lotto-prophet\\scripts\\3d\\3d_asc.csv', encoding=encoding)
                print(f"成功使用编码: {encoding}")
                print(f"数据形状: {df.shape}")
                print(f"列名: {df.columns.tolist()}")
                
                # 重命名列
                df = df.rename(columns={
                    '开奖期号': 'draw_issue',
                    '开奖日期': 'draw_date',
                    '1号': 'num_1',
                    '2号': 'num_2',
                    '3号': 'num_3'
                })
                
                # 保存处理后的数据
                output_path = 'g:\\python\\caipiao\\lotto-prophet\\scripts\\3d\\fc3d_history.csv'
                df.to_csv(output_path, encoding='utf-8', index=False)
                print(f"3D数据已处理并保存到: {output_path}")
                print(f"数据条数: {len(df)}")
                return True
            except Exception as e:
                print(f"使用编码 {encoding} 失败: {str(e)}")
                continue
        
        print("所有编码方式都失败了")
        return False
    except Exception as e:
        print(f"处理过程中出现错误: {str(e)}")
        return False

if __name__ == "__main__":
    fix_encoding()